package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        Long userID=BaseContext.getCurrentId();
        shoppingCart.setUserId(userID);

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userID);

        Long dishID = shoppingCart.getDishId();
        //如果在就数量加一
        if (dishID !=null){
            queryWrapper.eq(ShoppingCart::getDishId,dishID);
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品或者套餐是否在购物车中
        ShoppingCart One =shoppingCartService.getOne(queryWrapper);

        if (One!=null){
            Integer amout = One.getNumber();
            One.setNumber(amout+1);
            One.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(One);
        }else {
            //如果不在就保存

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            One = shoppingCart;
        }

        return R.success(One);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        List<ShoppingCart> list = new ArrayList<>();
        Long userID = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userID);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        list=shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);

        return R.success("情况购物车成功");

    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        Long dishID = shoppingCart.getDishId();

        if (dishID != null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart One =shoppingCartService.getOne(queryWrapper);
        Integer number = One.getNumber();
        if (number ==1){
            shoppingCartService.remove(queryWrapper);
        }else if (number>1){
            One.setNumber(One.getNumber()-1);
            shoppingCartService.updateById(One);
        }
        return R.success("删除成功！");
    }
}
